﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class FirstSearchController : Controller
    {
        // GET: FirstSearch
        public ActionResult Index()
        {
            DMComposite root = new DMComposite("Root");
            
            // 添加Root下的三个节点
            DMLeaf leaf1 = new DMLeaf("GameObject");
            DMLeaf leaf2 = new DMLeaf("GameObject (2)");
            DMComposite gameObject1 = new DMComposite("GameObject (1)");
            root.AddChild(leaf1);
            root.AddChild(gameObject1);
            root.AddChild(leaf2);

            // 添加GameObject (1)下面的两个节点
            DMLeaf child1 = new DMLeaf("GameObject_A");
            DMLeaf child2 = new DMLeaf("GameObject_B");
            gameObject1.AddChild(child1);
            gameObject1.AddChild(child2);

          
            Response.Write("Root<br/>");
            Response.Write("GameObject   GameObject (2)   GameObject (1) <br/>");
            Response.Write("GameObject_A  GameObject_A <br/>");

            // 按照广度优先或深度优先输出节点顺序
            DepthFirstSearch(root);
            Response.Write("<br/>");
            BreadthFirstSearch(root);



            return View();
        }
        // 广度优先检索
        private void BreadthFirstSearch(DMComponent component)
        {
            Queue<DMComponent> q = new Queue<DMComponent>();
            q.Enqueue(component);
            // 打印节点名字
            Response.Write(component.Name+"->");
          //  Debug.Log(component.Name);
            while (q.Count > 0)
            {
                DMComponent temp = q.Dequeue();
                List<DMComponent> children = temp.Children;
                foreach (DMComponent child in children)
                {
                    // 打印节点名字
                    Response.Write(child.Name + "->");
                   // Debug.Log(child.Name);
                    q.Enqueue(child);
                }
            }
        }

        // 深度优先检索
        private void DepthFirstSearch(DMComponent component)
        {
            // 打印节点名字
            Response.Write(component.Name + "->");
          //  Debug.Log(component.Name);
            List<DMComponent> children = component.Children;
            if (children == null || children.Count == 0) return;
            foreach (DMComponent child in children)
            {
                DepthFirstSearch(child);
            }
        }
    }
    // 抽象组件类
    public abstract class DMComponent
    {
        protected string mName;
        public string Name { get { return mName; } }
        public DMComponent(string name)
        {
            mName = name;
            mChildren = new List<DMComponent>();
        }
        protected List<DMComponent> mChildren;
        public List<DMComponent> Children { get { return mChildren; } }
        public abstract void AddChild(DMComponent c);
        public abstract void RemoveChild(DMComponent c);
        public abstract DMComponent GetChild(int index);
    }

    // 叶子组件类
    public class DMLeaf : DMComponent
    {
        public DMLeaf(string name) : base(name) { }


        public override void AddChild(DMComponent c)
        {
            return;
        }

        public override void RemoveChild(DMComponent c)
        {
            return;
        }

        public override DMComponent GetChild(int index)
        {
            return null;
        }
    }

    // 容器组件类
    public class DMComposite : DMComponent
    {
        public DMComposite(string name) : base(name) { }


        public override void AddChild(DMComponent c)
        {
            mChildren.Add(c);
        }

        public override void RemoveChild(DMComponent c)
        {
            mChildren.Remove(c);
        }

        public override DMComponent GetChild(int index)
        {
            return mChildren[index];
        }
    }                 
}